﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using UtilityExtensions;
using BOL;
using BOL.Comun;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.VentaAdmin
{
    public partial class ResumenVentaForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {

                    idProyecto.Value = (string)Request.QueryString["idProyecto"];
                    menuCtrl1.loadMenuVenta(idProyecto.Value.ToInt());
                    FormatForm();

                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }

        private void FormatForm()
        {
           



            var f = new ListValueBL().GetEtapaVentaXProyecto(idProyecto.Value.ToInt()).ToList();
            ListValueInfo todos = new ListValueInfo();
            todos.descripcion = "-----TODOS--------";
            todos.idInternal = 0;
            f.Insert(0, todos);


            tipoUIDDL.DataTextField = "descripcion";
            tipoUIDDL.DataValueField = "idInternal";


            var v = new BOL.ListValueBL().GetTipoUI(idProyecto.Value.ToInt());
            if (v.Count > 1)
            {
                v.Add(todos);
            }
            
            tipoUIDDL.DataSource = v;
            tipoUIDDL.DataBind();





            Chart071.LoadChart(idProyecto.Value.ToInt(), tipoUIDDL.SelectedValue.ToInt());
            //Chart091.LoadChart(idProyecto.Value.ToInt(),tipoUIDDL.SelectedValue.ToInt(),
            //                        entidadDDL.SelectedValue.ToInt() );

            //detalleCobranzaHLK.NavigateUrl = "~/VentaAdmin/OrdenVenta/PendienteCobranzaForm.aspx?idProyecto={0}".Fmt(idProyecto.Value);

            //resumenVentaHLK.NavigateUrl = "~/VentaAdmin/OrdenVenta/DetalleVentaForm.aspx?idProyecto={0}".Fmt(idProyecto.Value);
                                



        }


        private void LoadReport()
        {
            Chart071.LoadChart(idProyecto.Value.ToInt(),tipoUIDDL.SelectedValue.ToInt());
           // Chart091.LoadChart(idProyecto.Value.ToInt(),  tipoUIDDL.SelectedValue.ToInt(),entidadDDL.SelectedValue.ToInt());

        }

        protected void entidadDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadReport();

              
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);
            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);
            }
        }




        protected void tipoUIDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                LoadReport();
                

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        //protected void periodoDDL_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Chart061.LoadChart(idProyecto.Value.ToInt(), EsAcumuladoRBTN.SelectedValue.ToInt());
        //    }
        //    catch (SqlException sqlex)
        //    {
        //        WebServerSideSqlError.Log(sqlex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //         "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


        //    }
        //    catch (Exception ex)
        //    {
        //        WebServerSideError.Log(ex);
        //        ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
        //         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

        //    }
        //}

       
    }
}